package me.chanjar.weixin.cp.tp.service.impl;

import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.tp.service.WxCpTpMessageService;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Message.MESSAGE_SEND;

/**
 * <pre>
 *  Created by jamie on 2020/7/22.
 * </pre>
 */
@RequiredArgsConstructor
public class WxCpTpMessageServiceImpl implements WxCpTpMessageService {
  private final WxCpTpService mainService;


  @Override
  public WxCpMessageSendResult send(WxCpMessage message, String corpId) throws WxErrorException {
    String url = mainService.getWxCpTpConfigStorage().getApiUrl(MESSAGE_SEND)
      + "?access_token=" + mainService.getWxCpTpConfigStorage().getAccessToken(corpId);
    String responseContent = this.mainService.post(url, message.toJson());
    return WxCpMessageSendResult.fromJson(responseContent);
  }
}
